package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Insurance;
import cn.wolfcode.trip.base.mapper.InsuranceMapper;
import cn.wolfcode.trip.base.mapper.InsuranceProductMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IInsuranceService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class InsuranceServiceImpl implements IInsuranceService {
    @Autowired
    InsuranceMapper insuranceMapper;
    @Autowired
    InsuranceProductMapper insuranceProductMapper;

    // 分页查询+高级查询
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<?> list = insuranceMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public void saveOrUpdate(Insurance entity) {
        if (entity.getId() != null) {
            // 修改分类
            insuranceMapper.updateByPrimaryKey(entity);
        } else {
            // 新增分类
            insuranceMapper.insert(entity);
        }
    }

    @Override
    public void deleteById(Long id) {
        // 1、删除保险类型前，先删除insuranceproduct表中类型为id的产品
        insuranceProductMapper.deleteByInsuranceId(id);

        // 2、删除该保险类型
        insuranceMapper.deleteByPrimaryKey(id);
    }

    // 操作产品（增修）显示产品类型
    public List<Insurance> listAll() {
        return insuranceMapper.selectAll();
    }

}
